package com.iterator.service.web.goodsManager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.tcy.Comment;
import com.iterator.bean.web.pojo.tcy.CommentExample;
import com.iterator.bean.web.tcy.CommentListVO;
import com.iterator.mapper.web.goodsManager.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author tean_cy
 * @date 2021/1/11 12:26
 */

@Service
public class CommentServiceImpl implements CommentService{

    @Autowired
    CommentMapper commentMapper;

    @Override
    public CommentListVO list(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();

        if (!StringUtils.isEmpty(userId)){
        criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(valueId)){
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(false);

        commentExample.setOrderByClause(sort + " " + order);

        PageHelper.startPage(page,limit);
        List<Comment> items = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> goodsPageInfo = new PageInfo<>(items);
        long total = goodsPageInfo.getTotal();
        CommentListVO commentListVO = new CommentListVO(total, items);

        return commentListVO;
    }

    @Override
    public int delete(Comment commentDelete) {
        //逻辑删除，实际上是更新delete的状态
        Date date = new Date();
        //将时间和状态更新
        commentDelete.setUpdateTime(date);
        commentDelete.setDeleted(true);
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        // 根据 Id来更新
        int update = commentMapper.updateByPrimaryKey(commentDelete);
        return update;
    }
}
